<!-- 管理员个人中心 -->
<template slot-scope="scope">
  <div>
    <div class="container">
      <div v-show="isEdit" class="tcommonBox">
        <header>
          <h1>编辑个人资料</h1>
        </header>
        <section>
          <ul class="adminInfoBox">
            <li class="avatarlist">
              <span class="leftTitle">头像</span>
              <el-upload
                class="avatar-uploader"
                name="img"
                action="http://localhost:9999/admin/uploadImg"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img
                  v-if="uploadURL"
                  :src="uploadURL ? uploadURL : '@/assets/images/avatar.png'"
                  :onerror="$store.state.errorImg"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div slot="tip" class="el-upload__tip">
                  点击上传头像，只能上传jpg/png文件，且不超过1mb
                  <!-- 点击上传头像，只能上传png文件，且不超过1mb -->
                </div>
              </el-upload>
            </li>
            <li class="username">
              <span class="leftTitle">昵称</span>
              <el-input
                v-model="adminInfoObj.nickName"
                placeholder="昵称"
              ></el-input>
              <i class="fa fa-wa fa-asterisk"></i>
            </li>
            <li>
              <span class="leftTitle">电子邮件</span>
              <el-input
                v-model="adminInfoObj.email"
                placeholder="电子邮件"
              ></el-input>
              <i class="fa fa-wa fa-asterisk"></i>
            </li>
            <li>
              <span class="leftTitle">性别</span>
              <template>
                <el-radio class="radio" v-model="adminInfoObj.sex" label="1"
                  >男</el-radio
                >
                <el-radio class="radio" v-model="adminInfoObj.sex" label="0"
                  >女</el-radio
                >
              </template>
            </li>
          </ul>
          <div class="saveInfobtn">
            <a
              class="tcolors-bg"
              href="javascript:void(0);"
              @click="isEdit = !isEdit"
              >返 回</a
            >
            <a
              class="tcolors-bg"
              href="javascript:void(0);"
              @click="saveInfoFun"
              >保 存</a
            >
          </div>
        </section>
      </div>
      <div v-show="!isEdit" class="tcommonBox">
        <header>
          <h1>
            个人中心
            <span class="gotoEdit" @click="isEdit = !isEdit">
              <i class="fa fa-wa fa-edit"></i>
              编辑
            </span>
          </h1>
        </header>
        <section>
          <ul class="adminInfoBox">
            <li class="avatarlist">
              <span class="leftTitle">头像</span>
              <div class="avatar-uploader">
                <img
                  :src="
                    adminInfoObj.avatar
                      ? adminInfoObj.avatar
                      : '@/assets/images/avatar.png'
                  "
                  :onerror="$store.state.errorImg"
                  class="avatar"
                />
              </div>
            </li>
            <li class="username">
              <span class="leftTitle">昵称</span>
              <span>{{ adminInfoObj.nickName }}</span>
            </li>
            <li>
              <span class="leftTitle">电子邮件</span>
              <span>{{ adminInfoObj.email }}</span>
            </li>
            <li>
              <span class="leftTitle">性别</span>
              <span>{{ adminInfoObj.sex == 1 ? "男" : "女" }}</span>
            </li>
          </ul>
        </section>
      </div>
    </div>
  </div>
</template>

<script>
import { getInfo, saveAdminInfo } from "@/api/login.js"; //获取用户信息，保存用户信息
import store from "@/store/index.js";
export default {
  name: "adminInfo",
  data() {
    //选项 / 数据
    return {
      uploadURL: "",
      isEdit: false,
      adminInfo: {}, //本地存储的用户信息
      adminInfoObj: "", //用户的信息
    };
  },
  mounted() {
    this.getInfo();
  },
  methods: {
    //事件处理器
    handleAvatarSuccess(res, file) {
      console.log("imgsrc", res);
      //上传头像
      this.uploadURL = res.data;
      this.adminInfoObj.avator = this.uploadURL;
      console.log("img", this.uploadURL);
    },
    beforeAvatarUpload(file) {
      //判断头像大小
      const isJPG =
        file.type == "image/png" ||
        file.type == "image/jpg" ||
        file.type == "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 1;
      // console.log(file);
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/JPEG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
    getInfo() {
      getInfo()
        .then((res) => {
          console.log("res", res.user);
          this.adminInfoObj = res.user;
          console.log("admin", this.adminInfoObj);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    saveInfoFun: function () {
      //保存编辑的管理员信息
      var that = this;

      if (!that.adminInfoObj.nickName) {
        //昵称为必填
        that.$message.error("昵称为必填项，请填写昵称");
        return;
      }

      const postData = {
        email: this.adminInfoObj.email,
        headimg: this.uploadURL,
        id: this.adminInfoObj.id,
        nickname: this.adminInfoObj.nickName,
        sex: this.adminInfoObj.sex,
      };
      // console.log("----",headimg)
      console.log("ssss", postData);
      saveAdminInfo(postData)
        .then((response) => {
          console.log("ressss", response);
          //保存信息接口，返回展示页
          that.$message.success("保存成功！");
          that.isEdit = false;
          that.getInfo();
        })
        .catch((e) => {
          console.log(e);
        });
    },

    routeChange: function () {
      //展示页面信息
      var that = this;
      // console.log(this.$router,this.$route);
      if (localStorage.getItem("adminInfo")) {
        that.haslogin = true;
        that.adminInfo = JSON.parse(localStorage.getItem("adminInfo"));
        that.adminId = that.adminInfo.id;
      } else {
        that.haslogin = false;
      }
    },
  },
  watch: {
    // 如果路由有变化，会再次执行该方法
    $route: "routeChange",
  },
  created() {
    //生命周期函数
    this.routeChange();
    this.uploadURL = store.state.baseURL + "admin/uploadImg";
    console.log("sssss", this.uploadURL);
  },
};
</script>

<style>
body {
  font-family: Arial, "STHeiti", Helvetica, sans-serif;
  background: #efefef;
  font-size: 14px;
  color: #444 !important;
  min-height: 100vh;
  width: 100%;
  overflow-x: hidden;
  -webkit-overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100%;
  overflow-x: hidden;
  -webkit-overflow-x: hidden;
  color: #444;
}

h1,
h2 {
  font-weight: normal;
}
a {
  color: #444;
}
.container {
  max-width: 80%;
  margin: 0 auto;
  padding: 0 10px;
}

.tcommonBox {
  white-space: normal;
  word-wrap: break-word;
  word-break: break-all;
  position: relative;
  background: #fff;
  padding: 15px;
  border-radius: 5px;
  margin-bottom: 40px;
  font-size: 15px;
}
.tcommonBox header {
  white-space: normal;
  word-wrap: break-word;
  word-break: break-all;
}
.tcommonBox header h1 {
  margin: 10px 0;
  font-size: 25px;
  font-weight: 700;
  text-align: center;
  line-height: 30px;
}
.tcommonBox header h2 {
  margin: 10px 0;
  line-height: 24px;
  text-align: center;
  color: #555;
  font-size: 14px;
}
.tcommonBox section p {
  line-height: 24px;
  margin: 10px 0;
}
.tcommonBox header h2 .el-rate,
.rateBox {
  display: inline-block;
}
.tcommonBox header h2 .el-rate i {
  font-size: 16px;
}
.s-item .article-coontent {
  font-size: 15px;
  white-space: normal;
  word-wrap: break-word;
  word-break: break-all;
}
.s-item .article-content p {
  margin: 10px 0;
  line-height: 24px;
}
.s-item .viewdetail {
  margin: 10px 0;
  line-height: 24px;
  text-align: center;
}
.s-item .viewdetail a {
  color: #fff;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 5px;
}

.viewmore {
  text-align: center;
  width: 100%;
}
.viewmore a {
  /*background:  #97dffd;*/
  /*padding:6px 0;*/
  border-radius: 5px;
  text-align: center;
  font-size: 14px;
  color: #fff;
  height: 30px;
  line-height: 30px;
  display: block;
}
.tcommonBox {
  margin-top: 50px;
}

.adminInfoBox .avatarlist {
  position: relative;
}

.avatar-uploader {
  display: inline-block;
  vertical-align: top;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 120px;
  height: 120px;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  position: absolute;
  top: 0;
  left: 0;
}
.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: block;
  object-fit: cover;
}
.gotoEdit {
  font-size: 15px;
  float: right;
  cursor: pointer;
  color: #999;
}
.gotoEdit:hover {
  color: #333;
}
/*个人设置*/
.adminInfoBox .leftTitle {
  display: inline-block;
  width: 100px;
  padding: 10px 0;
}
.adminInfoBox .rightInner {
  display: inline-block;
  max-width: calc(100% - 140px);
  vertical-align: top;
}
.adminInfoBox li {
  padding: 20px;
  border-bottom: 1px solid #ddd;
}
.adminInfoBox li:last-child {
  border-bottom: 1px solid transparent;
}
.adminInfoBox .el-input,
.adminInfoBox .el-textarea {
  max-width: 300px;
  min-width: 100px;
}

.adminInfoBox .el-input__inner {
  border-radius: 4px;
}
.adminInfoBox .el-textarea {
  vertical-align: top;
}
.saveInfobtn {
  margin: 20px 0;
  text-align: center;
}
.saveInfobtn a {
  color: #fff;
  padding: 6px 20px;
  margin: 5px 10px;
  border-radius: 5px;
  font-size: 14px;
}
.adminInfoBox .fa-asterisk {
  color: #df2050;
}
.tcolors {
  color: #97dffd;
}
.tcolorm {
  color: #64609e;
}
.tcolord {
  color: #48456d;
}
.tcolors-bg {
  background: #97dffd;
  transition: all 0.3s ease-in-out;
}
.tcolord-bg {
  background: #48456d;
}
.tcolors-bg:hover {
  background: #48456d;
}

.paginationBox li.active {
  background: #64609e;
}
.paginationBox li:hover {
  color: #48456d;
}
.el-pagination button:hover {
  color: #48456d;
}

.fitImg {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.maxW {
  max-width: 100%;
}
.fwb {
  font-weight: bold;
}
.right {
  float: right;
}
.left {
  float: left;
}
.btnone {
  border-top: none !important;
}
.brone {
  border-right: medium none !important;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.smalltitle {
  /*height:36px;*/
  /*line-height: 36px;*/
}

.logoImg {
  width: 160px;
  height: 50px;
  display: inline-block;
  padding: 5px 5px;
  vertical-align: top;
}

.logoImg img {
  width: 100%;
  height: 100%;
}

.ui.label {
  border-color: #97dffd;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2),
    0 1px 5px 0 rgba(0, 0, 0, 0.12);
  background-color: #97dffd;
  margin: 5px 0 15px 2px;
  font-size: 14px;
  position: relative;
  left: -32px;
  border-radius: 0 4px 4px 0;
  padding: 7px 11.2px 7px 32px;
  display: inline-block;
  color: #fff;
}
.ui.label::after {
  position: absolute;
  content: "";
  top: 100%;
  left: 0;
  border-top: 0 solid transparent;
  border-right-width: 1em;
  border-right-color: #48456d;
  border-right-style: solid;
  border-bottom: 1em solid transparent;
  border-left: 0 solid transparent;
  width: 0;
  height: 0;
}
.ui.label a {
  color: #fff;
  line-height: 100%;
}
</style>
